package com.shengyin.ec.security;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.util.AntUrlPathMatcher;
import org.springframework.security.web.util.UrlMatcher;


public class ResourceDetailsMonitor implements InitializingBean {
    @Autowired
    private FilterSecurityInterceptor filterSecurityInterceptor;
    @Autowired
    private ResourceDetailsBuilder resourceDetailsBuilder;


    public void setResourceDetailsBuilder(
			ResourceDetailsBuilder resourceDetailsBuilder) {
		this.resourceDetailsBuilder = resourceDetailsBuilder;
	}


	public void setFilterSecurityInterceptor(
        FilterSecurityInterceptor filterSecurityInterceptor) {
        this.filterSecurityInterceptor = filterSecurityInterceptor;
    }


    protected UrlMatcher getUrlMatcher() {
        return new AntUrlPathMatcher();
    }

    public void afterPropertiesSet() {
        refresh();
    }

    public void refresh() {
        if (filterSecurityInterceptor != null) {
            FilterInvocationSecurityMetadataSource source = resourceDetailsBuilder
                .createUrlSource(getUrlMatcher());
            filterSecurityInterceptor.setSecurityMetadataSource(source);
        }

    }
}
